package fr.labri.macchiato.client.cart;

import com.smartgwt.client.widgets.IconButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import fr.labri.macchiato.client.cart.events.ValidateSearchHandler;
import fr.labri.macchiato.framework.business.finder.ProductFinder;
import fr.labri.macchiato.framework.business.service.Services;

/**
 * A widget searching and displaying a list of products corresponding to a
 * keyword
 * 
 * @author Matthieu FOUCAULT
 * 
 */
public abstract class ProductSearch extends VLayout {

	/**
	 * The {@link ProgressLabel} used during the search
	 */
	protected ProgressLabel progressbar;
	/**
	 * The button to valid the search when one or more results are selected
	 */
	protected IconButton validButton;
	/**
	 * The button to cancel the search and hide the widget
	 */
	protected IconButton cancelButton;
	/**
	 * The layout where the buttons are placed
	 */
	protected HLayout buttonsLayout;
	/**
	 * The keyword used for the search
	 */
	protected String searchKeyword;
	/**
	 * The label displaying the number of results
	 */
	protected Label resultNumber;
	/**
	 * The form used to select the result of the search
	 */
	protected DynamicForm contents;

	/**
	 * Create a widget searching products for the specified keyword
	 * 
	 * @param searchKeyword The keyword used for the search
	 */
	ProductSearch(String searchKeyword) {
		this.searchKeyword = searchKeyword;
		resultNumber = new Label();
		resultNumber.setHeight(30);
		resultNumber.setMargin(10);
		contents = new DynamicForm();
		initButtons();
	}

	/**
	 * Add a {@link ValidateSearchHandler} on the widget
	 * 
	 * @param handler The handler to be added
	 */
	abstract void addValidateHandler(final ValidateSearchHandler handler);

	/**
	 * Starts the search and display the results
	 */
	abstract void startSearch();

	/**
	 * Initialize the different buttons of the widget
	 */
	protected void initButtons() {
		buttonsLayout = new HLayout(15);
		validButton = new IconButton("Valider");
		cancelButton = new IconButton("Annuler");
		cancelButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				hide();
//				destroy();
			}
		});
		validButton.setIcon("/icons/validIcon.svg");
		cancelButton.setIcon("/icons/cancelIcon.png");
		buttonsLayout.addMember(validButton);
		buttonsLayout.addMember(cancelButton);
	}

	/**
	 * Display the widget
	 */
	protected void displayContents() {
		progressbar.destroy();
		addMember(resultNumber);
		addMember(contents);
		addMember(buttonsLayout);
	}

	/**
	 * Helper to get the service {@link ProductFinder}
	 * 
	 * @return The instance of the service ProductFinder
	 */
	protected ProductFinder getProductFinder() {
		return Services.getInstance().getProductFinder();
	}

}